package xyz.bali16.module.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import com.google.common.base.Preconditions;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.exception.BusinessException;
import xyz.bali16.application.core.utils.AuthUtils;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.service.UserService;
import xyz.bali16.module.article.entity.Article;
import xyz.bali16.module.article.entity.Like;
import xyz.bali16.module.article.mapper.LikeMapper;
import xyz.bali16.module.article.model.collection.CollectionPageParam;
import xyz.bali16.module.article.model.like.LikeAddParam;
import xyz.bali16.module.article.model.like.LikePageParam;
import xyz.bali16.module.article.service.ArticleRankServiceCaller;
import xyz.bali16.module.article.service.ArticleService;
import xyz.bali16.module.article.service.CollectionService;
import xyz.bali16.module.article.service.LikeService;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {
    @Autowired
    @AutowiredType(AutowiredType.Type.PLUGIN_MAIN)
    UserService userService;
    @Autowired
    LikeService likeService;
    @Autowired
    ArticleRankServiceCaller articleRankServiceCaller;
    @Autowired
    CollectionService collectionService;
    @Autowired
    ArticleService articleService;
    @Override
    public IPage<Like> getPage(LikePageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getLikePage(page, param.getTargetId(),param.getUserId());
    }

    @Override
    public void addLike(LikeAddParam param) throws Exception {

        Preconditions.checkNotNull(param, "LikeAddParam 参数不能为为空");
        Like like = new Like();
        BeanUtils.copyProperties(param, like);
        System.out.println(like);
        if(isExist(like.getTargetId())){
            throw new BusinessException("已点赞过");
        }

        save(like);
        // 处理点赞数量
        LikePageParam likePageParam = new LikePageParam();
        likePageParam.setTargetId(likePageParam.getTargetId());
        likePageParam.setCurrent(0);
        likePageParam.setSize(10);
        CollectionPageParam collectionPageParam = new CollectionPageParam();
        collectionPageParam.setTargetId(param.getTargetId());
        collectionPageParam.setCurrent(0);
        collectionPageParam.setSize(10);
        Integer collectNum = (int) collectionService.getPage(collectionPageParam).getTotal();
        Integer likeNum = (int)likeService.getPage(likePageParam).getTotal();
        articleRankServiceCaller.updateTypeCount("like",like.getTargetId(),likeNum);
        Article article = articleService.getById(param.getTargetId());
        articleRankServiceCaller.incrTypeCountAndUnionForWeekRank("like", article.getId(), article.getTitle(), article.getCommentCount(), article.getViews(), article.getGmtCreated(), likeNum, collectNum, true);
    }

    @Override
    public void delete(String likeId) throws Exception {
        if (likeId == null) {
            return;
        }
        User user = userService.getByUsername(AuthUtils.getCurrentUsername());
        Wrapper<Like> wrapper = Wrappers.<Like>lambdaQuery().eq(Like::getTargetId,likeId).eq(Like::getUserId,user.getUserId());
        remove(wrapper);
        // 处理点赞数量
        LikePageParam likePageParam = new LikePageParam();
        likePageParam.setTargetId(likePageParam.getTargetId());
        likePageParam.setCurrent(0);
        likePageParam.setSize(10);
        CollectionPageParam collectionPageParam = new CollectionPageParam();
        collectionPageParam.setTargetId(likeId);
        collectionPageParam.setCurrent(0);
        collectionPageParam.setSize(10);
        Integer collectNum = (int) collectionService.getPage(collectionPageParam).getTotal();
        Integer likeNum = (int)likeService.getPage(likePageParam).getTotal();
        articleRankServiceCaller.updateTypeCount("like",likeId,likeNum);
        Article article = articleService.getById(likeId);
        articleRankServiceCaller.incrTypeCountAndUnionForWeekRank("like", article.getId(), article.getTitle(), article.getCommentCount(), article.getViews(), article.getGmtCreated(), likeNum, collectNum, false);
    }

    public boolean isExist(String targetId){
        Wrapper<Like> wrapper = Wrappers.<Like>lambdaQuery().eq(Like::getTargetId,targetId);
        Like like = this.getOne(wrapper);
        return like!=null;
    }
}



